package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/elastic/go-elasticsearch/v7"
	"log"
	"os"
	"time"
)

var client *elasticsearch.Client

var count = 0

type ShopInfo2 struct {
	Id       int64  `json:"id"`
	Name     string `json:"name"`
	Age      int32  `json:"age"`
	ShopName string `json:"body"`
}

func init() {
	config := elasticsearch.Config{
		Addresses: []string{
			"http://47.96.16.226:9200",
		},
	}
	elasticClient, err := elasticsearch.NewClient(config)
	if err != nil {
		os.Exit(-1)
	}
	client = elasticClient
}

func main() {

	go statCount()

	for {
		body := &bytes.Buffer{}
		_ = json.NewEncoder(body).Encode(&ShopInfo2{
			Name:     "songxiaohui",
			Age:      23,
			ShopName: "shopName",
		})
		index, err := client.Index("shop_info", body)
		if err != nil {
			fmt.Println(err)
			continue
		}
		fmt.Println(index)
		count++

	}
}

func statCount() {
	ticker := time.NewTicker(1 * time.Second)
	lastCount := count
	for range ticker.C {
		nowCount := count
		log.Println("[Stat] >>>>>> ", time.Now().Format("2006-01-02 15:04:05"), " diff ", nowCount-lastCount)
		lastCount = nowCount
	}
	ticker.Stop()
}
